package com.al.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.al.classs.Location;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@WebServlet("/location")
public class LocationServlet extends HttpServlet {
	
	private static Map<String,List<String>> cityMap;
	private static Map<String,List<String>> provinceMap;
	static {
		cityMap = new HashMap<>();
		//西安
		List<String> areas = new ArrayList<>();
		areas.add("雁滩区");
		areas.add("莲湖区");
		areas.add("新城区");
		cityMap.put("西安市",areas);
		//宝鸡
		areas = new ArrayList<>();
		areas.add("陈仓区");
		areas.add("渭滨区");
		areas.add("新城区");
		cityMap.put("宝鸡市",areas);
		//渭南
		areas = new ArrayList<>();
		areas.add("临渭区");
		areas.add("高新区");
		cityMap.put("渭南市", areas);
		//郑州
		areas = new ArrayList<>();
		areas.add("郑州A区");
		areas.add("郑州B区");
		cityMap.put("郑州市", areas);
		//洛阳
		areas = new ArrayList<>();
		areas.add("洛阳A区");
		areas.add("洛阳B区");
		cityMap.put("洛阳市", areas);
		
		provinceMap = new HashMap<>();
		List<String> citys = new ArrayList<>();
		citys.add("西安市");
		citys.add("宝鸡市");
		citys.add("渭南市");
		provinceMap.put("陕西省",citys);
		citys = new ArrayList<>();
		citys.add("郑州市");
		citys.add("洛阳市");
		citys.add("开封市");
		provinceMap.put("河南省",citys);
		citys = new ArrayList<>();
		citys.add("南京市");
		citys.add("苏州市");
		citys.add("南通市");
		provinceMap.put("江苏省",citys);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String type = req.getParameter("type");
		resp.setCharacterEncoding("UTF-8");
		String id = req.getParameter("id");
		switch(type) {
		case "city":
			List<String> areas = cityMap.get(id);
			JSONArray jsonArray = JSONArray.fromObject(areas);
			resp.getWriter().write(jsonArray.toString());
			break;
		case "province":
			List<String> citys = provinceMap.get(id);
			String city = citys.get(0);
			List<String> cityAreas = cityMap.get(city);
			Location location = new Location();
			location.setAreas(cityAreas);
			location.setCitys(citys);
			JSONObject jsonObject = JSONObject.fromObject(location);
			resp.getWriter().write(jsonObject.toString());
			break;
		}
	}
}
